package com.zsy.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cdg.bean.ZtreeRole;
import com.cdg.util.GetDate;
import com.cdg.util.Json;
import com.cdg.util.PrimaryId;
import com.xbw.bean.Bis_shop_post;
import com.xbw.bean.Sys_Menu;
import com.zsy.mapper.Bis_Shop_PostMapper;

@Service
@Transactional
public class Bis_Shop_PostServiceImpl  implements Bis_Shop_PostService{
	@Autowired
	private Bis_Shop_PostMapper bis_Shop_PostMapper;

	@Override
	@Transactional(propagation=Propagation.SUPPORTS)
	public Bis_shop_post getByPost_code(String post_code) {
		return bis_Shop_PostMapper.getByPost_code(post_code);
	}

	@Override
	@Transactional(propagation=Propagation.SUPPORTS)
	public List<Bis_shop_post> listAll(Map map) {
		return bis_Shop_PostMapper.listAll(map);
	}

	/** 
	* TODO �������÷�����ʵ�ֹ��ܣ���ѡ��.  ztree�˵�
	* @see com.zsy.service.Bis_Shop_PostService#listMenu() 
	*/  
	@Override
	@Transactional(propagation=Propagation.SUPPORTS)
	public String listMenu(String id) {
		List<Sys_Menu> menu=bis_Shop_PostMapper.listMenu();
		List<ZtreeRole> listztree=new ArrayList<>();
		for(Sys_Menu m:menu){
			ZtreeRole ztree=new ZtreeRole();
			ztree.setId(m.getMenu_id());
			ztree.setpId(m.getParent_id());
			ztree.setName(m.getMenu_name());
			ztree.setOpen((m.getIs_auto_expand()=="0"?false:true));
			int count=bis_Shop_PostMapper.CountMenu(id,m.getMenu_id());
			ztree.setChecked(count>0?true:false);
			listztree.add(ztree);
		}
		  //������ת����json��
		String json=Json.getJson(listztree);
		return json;
	}

	/** 
	* TODO �������÷�����ʵ�ֹ��ܣ���ѡ��. 
	* @see com.zsy.service.Bis_Shop_PostService#save(com.xbw.bean.Bis_shop_post) 
	*/  
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void save(Bis_shop_post post) {
		String data=post.getPost_id();
		post.setPost_id(PrimaryId.keyId());
		post.setCreate_time(GetDate.getNetworkTime());
		bis_Shop_PostMapper.save(post);
        if(data!=null&&data!=""){
        	Map<String,Object> map =new HashMap<String,Object>();
        	String[] str=data.split(",");
        	map.put("post_id",post.getPost_id());
        	map.put("menu_id", str);
        	map.put("create_time",post.getCreate_time());
        	map.put("create_user_id", post.getCreate_user_id());
			post.setPost_name(data);
			bis_Shop_PostMapper.savepostM(map);
		}
	}

	/** 
	* TODO �������÷�����ʵ�ֹ��ܣ���ѡ��. 
	* @see com.zsy.service.Bis_Shop_PostService#getByPostId(java.lang.String) 
	*/  
	@Override
	@Transactional(propagation=Propagation.SUPPORTS)
	public Bis_shop_post getByPostId(String id) {
		return bis_Shop_PostMapper.getByPostId(id);
	}

	/** 
	* TODO �������÷�����ʵ�ֹ��ܣ���ѡ��. 
	* @see com.zsy.service.Bis_Shop_PostService#update(com.xbw.bean.Bis_shop_post) 
	*/  
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void update(Bis_shop_post post) {
		post.setCreate_time(GetDate.getNetworkTime());
		bis_Shop_PostMapper.update(post);
		bis_Shop_PostMapper.del(post.getPost_id());
        if(post.getModify_user_id()!=null||post.getModify_user_id()!=""){
        	Map<String,Object> map =new HashMap<String,Object>();
        	String[] str=post.getModify_user_id().split(",");
        	map.put("post_id",post.getPost_id());
        	map.put("menu_id", str);
        	map.put("create_time",post.getCreate_time());
        	map.put("create_user_id", post.getCreate_user_id());
    		bis_Shop_PostMapper.savepostM(map);
	    }
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void del(String post_id) {
		bis_Shop_PostMapper.del(post_id);
		
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void delete(String post_id) {
		bis_Shop_PostMapper.delete(post_id);
	}


	
}
